package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教育局行政办公 外部通知 搜索通知
 * 
 */
@CrossOrigin
@RestController("OuterNoticeSendSrhController")
public class OuterNoticeSendSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository YxyNoticeOutterRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/send/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        // 当前教育局id
        String edbId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        // 未删除状态
        int deleteStatus = 0;
        // 发件人列表
        Page<YxyNoticeOutter> page = YxyNoticeOutterRepository.findByUserIdAndEdbIdAndDeleteStatus(userId, edbId, deleteStatus, pageable);

        List<RespModel> respModelLst = new ArrayList<RespModel>();
        for (YxyNoticeOutter yxyNoticeOutter : page.getContent()) {
            // JAVA 8 查询所有接受用户信息.多个用户以逗号分割，出现异常，返回空字符串
            String receiveSchoolName = yxyNoticeOutter.getAllReceiveSchoolName(",").orElse("");
            RespModel respModel = new RespModel();
            respModel.setId(yxyNoticeOutter.getId());
            respModel.setTitle(yxyNoticeOutter.getTitle());
            respModel.setCreatedDate(DateToolUtils.format(yxyNoticeOutter.getCreatedDate(), "yyyy-MM-dd"));
            respModel.setReceiveSchoolName(receiveSchoolName);
            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        private String id;
        // 消息标题
        private String title;
        // 发布时间
        private String createdDate;
        // 接收学校昵称
        private String receiveSchoolName;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getCreatedDate() {
            return createdDate;
        }

        public void setCreatedDate(String createdDate) {
            this.createdDate = createdDate;
        }

        public String getReceiveSchoolName() {
            return receiveSchoolName;
        }

        public void setReceiveSchoolName(String receiveSchoolName) {
            this.receiveSchoolName = receiveSchoolName;
        }
    }
}
